package com.hzdl.cinema.dao.impl;

import com.hzdl.cinema.dao.StudioDao;
import com.hzdl.cinema.domain.Studio;
import com.hzdl.cinema.util.JDBCUtils;
import org.junit.Test;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;

import java.util.List;

public class StudioDaoImpl implements StudioDao {

    JdbcTemplate jdbcTemplate=new JdbcTemplate(JDBCUtils.getDataSource());

    @Override
    public List<Studio> findAll() {
        String sql="select * from studio";
        List<Studio> studioList = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(Studio.class));
        return studioList;
    }

    @Override
    public Studio findStudioById(int studioId) {
        String sql="select * from studio where studio_Id=?";
        Studio studio=null;
        try {
            studio = jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper<>(Studio.class), studioId);
        }catch (Exception e){
//            e.printStackTrace();
        }
        return studio;
    }

    @Override
    public Studio findStudioByName(String studioName) {
        String sql="select * from studio where studio_Name=?";
        Studio studio=null;
        try {
            studio = jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper<>(Studio.class), studioName);
        }catch (Exception e){
//            e.printStackTrace();
        }
        return studio;
    }

    @Override
    public void addStudio(String studioName, Integer studioColcount, Integer studioRowcount) {
        String sql="insert into studio values(null,?,?,?)";
        jdbcTemplate.update(sql,studioName,studioRowcount,studioColcount);
    }

    @Override
    public void editStudio(int studioId, String studioName, Integer studioColcount, Integer studioRowcount) {
        String sql="update studio set studio_Name = ?,studio_RowCount=?,studio_ColCount=? where studio_Id = ?";
        jdbcTemplate.update(sql,studioName,studioRowcount,studioColcount,studioId);
    }

    @Override
    public void deleteStudioById(int studioId) {
        String sql="delete from studio where studio_Id = ? ";
        jdbcTemplate.update(sql,studioId);
    }

    @Override
    public Studio searchStudioByName(String studioName) {
        String sql="select * from studio where studio_Name=?";
        Studio studio=null;
        try {
            studio = jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper<>(Studio.class), studioName);
        }catch (Exception e){
//            e.printStackTrace();
        }
        return studio;
    }

    @Override
    public Studio findMyStudio(Integer studioId) {
        String sql = "select * from studio where studio_Id=?";
        Studio studio = jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper<>(Studio.class), studioId);
        return studio;
    }
	
	
    @Override
    public void modifyStudioName(Integer scheduleId, String studioName) {
        String sql ="update studio set studio_Name=? where studio_Id in(select studio_Id from schedule where schedule_Id = ?)";
        jdbcTemplate.update(sql,studioName,scheduleId);
    }

    @Override
    public int findIdByName(String studioName) {
        String sql="select studio_Id from studio where studio_Name=?";
        int studioId=jdbcTemplate.queryForObject(sql,Integer.class,studioName);
        return studioId;
    }


}
